package com.test.stuff;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Group{
	
	protected String name;
	protected int id;
	protected List<Account> accounts;
	
	protected static Logger logger = new Logger(Group.class.getSimpleName());
	
	//////
	// constructors
	
	public Group(String name, int id, Account[] accounts) throws Exception{
		this(name, id, convertAccountArrayToList(accounts));
	}
	
	public Group(String name, int id, List<Account> accounts) throws Exception{
		if(name==null || name.isEmpty())
			throw new Exception("Cannot init Event because name is invalid");
		if(id<1)
			throw new Exception("Cannot init Event because id is invalid");
		if(accounts==null || accounts.isEmpty())
			throw new Exception("Cannot init Event because accounts is invalid");
		
		this.name = name;
		this.id = id;
		this.accounts = accounts;
	}
	
	//////
	// overrides
	
	@Override
	public int hashCode(){
		int prime = 41;
		return (int)(Math.pow(name.length(), ((id*prime)+Math.pow(accounts.hashCode(), prime))));
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this==obj)
			return true;
		
		if(obj==null){
//	    	logger.log("obj null");
			return false;
		}
		
		if (this.getClass() != obj.getClass()) {
//	    	logger.log("classes() don't match");
//	    	logger.log(this.getClass()+" vs "+obj.getClass());
	        return false;
	    }
		
	    final Group other = (Group) obj;
	    // if this name is null, then the other name should be null
	    // otherwise check if the names are the same
	    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
//	    	logger.log("name don't match");
//	    	logger.log(this.name+" vs "+other.name);
	        return false;
	    }
	    
	    // if the accounts is null, then the other accounts should be null
	    // otherwise check if the accounts are the same
	    if(this.accounts == null ? other.accounts != null : !this.accounts.equals(other.accounts)){
//	    	logger.log("accounts don't accounts");
//	    	logger.log(this.accounts+" vs "+other.accounts);
	    	return false;
	    }
	    
	    // finally verify id is the same value
	    if (this.id != other.id) {
//	    	logger.log("id don't match");
//	    	logger.log(this.id+" vs "+other.id);
	        return false;
	    }
	    
	    // if name and id are the same, then they are equal
	    return true;
	}
	
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		
		result.append("Group Details -- ");
		result.append("Name: "+this.name+", ");		
		result.append("ID: "+this.id+", ");
		result.append("Accounts: "+this.accounts);

		
		return result.toString();
	}
	
	//////
	// setters
	
	public void setName(String name){
		if(name==null || name.isEmpty()){
			return;
		}
		this.name = name;
	}

	public void setId(int id){
		if(id<0){
			return;
		}
		this.id = id;
	}
	
	public void setAccounts(Account[] accounts){
		setAccounts(convertAccountArrayToList(accounts));
	}
	
	public void setAccounts(List<Account> accounts){
		if(accounts==null || accounts.isEmpty()){
			return;
		}
		this.accounts = accounts;
	}
	
	//////
	// getters
	
	public String getName(){ return this.name; }
	public int getId(){ return this.id; }
	public List getAccounts(){ return this.accounts; }
	
	//////
	// methods

	public static long getCurrentTime(){
		return Calendar.getInstance().getTimeInMillis();
	}
	
	public static List<Account> convertAccountArrayToList(Account[] accounts){
		if(accounts==null)
			return null;
		
		List<Account> accountList = new ArrayList();
		for(Account account : accounts)
			accountList.add(account);
		
		return accountList;
	}
}